# -*- coding: utf-8 -*-

#   Copyright (C) 2011  P.L. Lucas, P.L. del Castillo
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pygtk
pygtk.require('2.0')
import gtk
import cairo
import os
import os.path
from fx import *
from transitions import *

class Picture:
	def __init__(self, path, duration, videoProperties):
		self.videoProperties=videoProperties
		if path!=None:
			self.set_color(-1.0,-1.0,-1.0)
		else:
			self.set_color(0.0,0.0,0.0)
		self.path=path
		self.duration=duration
		self.effect=[FX(videoProperties, duration)]
		self.transition=TransitionFade(videoProperties, 2.0)
		self.demo_pixbuf=None
		
	def set_color(self, red, green, blue):
		self.red=red
		self.green=green
		self.blue=blue
		self.path=None
	
	def get_color(self):
		return self.red, self.green, self.blue
		
	
	def xml(self):
		x='<picture>\n'
		x=x+'\t<duration>'+str(self.duration)+'</duration>\n'
		if self.path==None:
			x=x+'\t<red>'+str(self.red)+'</red>\n'
			x=x+'\t<green>'+str(self.green)+'</green>\n'
			x=x+'\t<blue>'+str(self.blue)+'</blue>\n'
		else:
			x=x+'\t<path>'+os.path.abspath(self.path)+'</path>\n'
		for f in self.effect:
			x=x+f.xml()
		if self.transition!=None:
			x+=self.transition.xml()
		x=x+'</picture>\n'
		return x
	
	def set_duration(self, duration):
		self.duration=duration
		for fx in self.effect:
			fx.set_duration(duration)

	def get_pixbuf_icon(self):
		pix=self.get_pixbuf_from_path(self.path, 64,64)
		i=0
		for f in self.effect:
			pix=f.icon(pix,i)
			i=i+1
		if self.transition!=None:
			pix=self.transition.icon(pix)
		return pix
		
		
	def get_pixbuf(self, width=-1, height=-1):
		if self.demo_pixbuf==None:
				self.demo_pixbuf=self.get_pixbuf_from_path(self.path, 100, 100)
		if self.videoProperties.demoOk:
			return self.get_pixbuf_from_pixbuf(self.demo_pixbuf, width, height)
		else:
			return self.get_pixbuf_from_path(self.path, width, height)
	
	def get_pixbuf_from_path(self, path, width=-1, height=-1):
		pixbuf=None
		if path==None:
			pixbuf=image_color(64, 64, self.red, self.green, self.blue)
		else:
			pixbuf=gtk.gdk.pixbuf_new_from_file(self.path)
		return self.get_pixbuf_from_pixbuf(pixbuf, width, height)
	
	def get_pixbuf_from_pixbuf(self, pixbuf, width=-1, height=-1):
		if width<=0 or height<=0:
			return pixbuf
		scale_x=1.0
		scale_y=float(pixbuf.get_height())/float(pixbuf.get_width())
		if pixbuf.get_width()<pixbuf.get_height():
			scale_x=float(pixbuf.get_width())/float(pixbuf.get_height())
			scale_y=1.0
		width=int(width*scale_x)
		height=int(height*scale_y)
		return pixbuf.scale_simple(width, height,gtk.gdk.INTERP_BILINEAR)
	
	def get_path(self):
		return self.path
	
	def get_row(self):
		return [self.get_pixbuf_icon(), self.duration, self, 0.0]
	
	def render_start(self):
		self.pixbuf=self.get_pixbuf()
		for fx in self.effect:
			fx.render_start()
		
	def render(self, t):
		pix=self.pixbuf
		for fx in self.effect:
			pix=fx.render(pix, t)
		return pix
	
	def render_end(self):
		self.pixbuf=None
		for fx in self.effect:
			pix=fx.render_end()
	
	def init_from_data(self, attrs, effects, transition, videoProperties):
		if not 'path' in attrs:
			attrs['path']=None
		self.__init__(attrs['path'], float(attrs['duration']), videoProperties)
		self.effect=[]
		for e in effects:
			f=fx_list()[e['name']](self.videoProperties, self.duration)
			f.init_from_data(e)
			self.effect.append(f)
		if len(transition)>0:
			f=transitions_list()[transition['name']](self.videoProperties, self.duration)
			f.init_from_data(transition)
			self.transition=f
		else:
			self.transition=None
		if attrs['path']==None:
			self.set_color(float(attrs['red']), float(attrs['green']), float(attrs['blue']))

class VideoProperties:
	def __init__(self, width, height, framespersecond=25):
		self.width=int(width)
		self.height=int(height)
		self.framespersecond=int(framespersecond)
		self.sound_path=''
		self.demoOk=False
	def xml(self):
		x='<videoProperties>\n'
		x=x+'\t<width>'+str(self.width)+'</width>\n'
		x=x+'\t<height>'+str(self.height)+'</height>\n'
		x=x+'\t<framespersecond>'+str(self.framespersecond)+'</framespersecond>\n'
		x=x+'\t<sound_path>'+str(self.sound_path)+'</sound_path>\n'
		x=x+'</videoProperties>\n'
		return x
	def init_from_data(self, attrs):
		#attrs es un diccionario con las propiedades
		for k,v in attrs.iteritems():
			if len(v)>0 and v[0].isdigit():
				i='self.'+k+'='+v
			else:
				i='self.'+k+'="'+v+'"'
			x=compile(i, '<string>', 'exec')
			eval(x)
